package moxl.xpath.functions;

import moxl.xpath.XPathContext;
import moxl.xpath.functions.XPathFunctionIllegalArgumentException;
import moxl.xpath.parser.states.step.Function;
import moxl.xpath.parser.types.XPathNumberValue;
import moxl.xpath.parser.types.AbstractXPathValue;

public class AddFunction implements Function
{
	public AbstractXPathValue execute(XPathContext context, AbstractXPathValue... args) throws XPathFunctionIllegalArgumentException
	{
		double total = 0;
		int argIndex = 0;
		for (AbstractXPathValue arg : args)
		{
			if (arg instanceof XPathNumberValue)
			{
				XPathNumberValue number = (XPathNumberValue) arg;
				total += number.getValueAsDouble();
			}
			else
			{
				throw new XPathFunctionIllegalArgumentException(this, argIndex, arg, XPathNumberValue.class);
			}
			argIndex++;
		}
		return new XPathNumberValue(total);
	}

	public String getName()
	{
		return "add";
	}

}
